package com.qf.controller;/*
 *保护你的大熊
 *2021/11/27
 *21:11
 */

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.AliyunOSSUtil;
import com.qf.util.ResponseUtil;
import com.qf.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin("*")
@RestController
@RequestMapping("/admin/storage")
public class AdminStorageController {

    @Autowired
    DtsStorageService storageService;



    @RequestMapping("/create")
    public Object add(MultipartFile file) throws Exception {

        //截取后缀名   qwer.jpg
        String originalFilename = file.getOriginalFilename();//获取原始文件名
        String ext = originalFilename.substring(originalFilename.lastIndexOf("."));

        DtsStorage storage = storageService.insert(ext,(int)file.getSize(),file.getBytes());
        return ResponseUtil.ok(storage);

    }


    @GetMapping("/list")
    public Object list(
            @RequestParam(name = "page",required = false,defaultValue = "1") Integer page,
            @RequestParam(name = "limit",required = false,defaultValue = "20") Integer limit,
            String key,
            String name,
            @RequestParam(name = "sort",required = false,defaultValue = "add_time") String sort,
            @RequestParam(name = "order",required = false,defaultValue = "asc") String order
    ){

        IPage<DtsStorage> iPage = storageService.queryPage(page,limit,key,name,sort,order);

        Map<String,Object> data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());
        return ResponseUtil.ok(data);

    }
}
